package com.example.sourceslibs

import android.util.Log

interface ICarManager {

    interface ICallback{
        fun onSignalChange(signalType: SignalType,value:Any?)
    }

    fun registerListener(callback:ICallback,signalList:List<SignalType>)

    fun unregisterListener(callback: ICallback)


    fun setSignalForObject(signalType: SignalType,state:Any):Boolean

    fun getSignalForObject(signalType: SignalType):Any?

    fun dispatchSignalChange(signalType: SignalType,value: Any?)
}


enum class SignalType{
    CAR_VEHICLE_GEAR,
    CAR_SKY_WINDOW_POS,
    CAR_SKY_WINDOW_STATE,
    CAR_TAIL_GATE_POS,
    CAR_TAIL_GATE_STATE,
}



class CarManagerImpl:ICarManager{
    private val TAG = "CarManagerImpl"
    private val callbackList = ArrayList<ICarManager.ICallback>()
    override fun registerListener(callback: ICarManager.ICallback, signalList: List<SignalType>) {
        Log.i(TAG, "registerListener: ")
        callbackList.add(callback)
    }

    override fun unregisterListener(callback: ICarManager.ICallback) {
        callbackList.remove(callback)
    }

    override fun setSignalForObject(signalType: SignalType, state: Any): Boolean {
        when(signalType){
            SignalType.CAR_SKY_WINDOW_POS->{
                //模拟window pos
                dispatchSignalChange(SignalType.CAR_SKY_WINDOW_STATE,0)
                if(state is Int){
                    dispatchSignalChange(signalType,state)
                }
            }
            else->{
                //do nothing
            }

        }


        return true
    }

    override fun getSignalForObject(signalType: SignalType): Any? {
        return null
    }

    override fun dispatchSignalChange(signalType: SignalType, value: Any?) {
        Log.i(TAG, "dispatchSignalChange: ")
        for (iCallback in callbackList) {
            Log.i(TAG, "dispatchSignalChange: $signalType $value")
            iCallback.onSignalChange(signalType, value)
        }
    }

}